<template>
  <div class="todoapp">
    <TodoHeader @getname="addList" :arr="list"></TodoHeader>
    <TodoMain :arr="showArr" @delFfn="delFn"></TodoMain>
    <TodoFooter
      :arr="showArr"
      @changeType="changeFn"
      @clearType="clearFn"
    ></TodoFooter>
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      cons: "all",
      list: JSON.parse(localStorage.getItem("todo")) || [],
      // list: [
      //   { id: 100, name: "吃饭", isDone: true },
      //   { id: 201, name: "睡觉", isDone: false },
      //   { id: 103, name: "打豆豆", isDone: true },
      // ],
    };
  },
  methods: {
    addList(thename) {
      this.list.push({
        id:
          this.list.length === 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name: thename,
        isDone: false,
      });
    },
    delFn(tId) {
      // this.list.splice(ind, 1);   此方法会有bgm
      // 方法二
      // this.list.splice(
      //   this.list.findIndex((obj) => obj.id == tId),
      //   1
      // );
      this.list.forEach((obj) => {
        if (obj.id == tId) {
          this.list.splice(this.list.indexOf(obj), 1);
        }
      });
    },
    changeFn(conStr) {
      this.cons = conStr;
    },
    clearFn() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  watch: {
    list: {
      handler(newval) {
        localStorage.setItem("todo", JSON.stringify(newval));
      },
      deep: true,
    },
  },
  computed: {
    showArr() {
      if (this.cons === "ok") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.cons === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
};
</script>

<style>
</style>